package com.egd.imapp.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.egd.imapp.R;

public class CustomDialog extends AlertDialog {
    private TextView tittle, msg; //标题，消息
    private Button negative, positive; //取消和确认按钮
    private String sTittle, sMsg, sNegative, sPositive;

    public CustomDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);
        initView();                                         //初始化控件
        initEvent();                                        //初始化控件的点击事件
    }

    private void initView(){
        tittle=findViewById(R.id.tittle);
        msg=findViewById(R.id.msg);
        negative=findViewById(R.id.negative);
        positive=findViewById(R.id.positive);
    }
    //初始化控件的显示数据
    private void refreshView(){
        //如果自定义了标题和消息，则直接显示
        if (!TextUtils.isEmpty(sTittle)){
            tittle.setText(sTittle);
            tittle.setVisibility(View.VISIBLE);
        }else {
            tittle.setVisibility(View.GONE);        //隐藏标题
        }
        if (!TextUtils.isEmpty(sMsg)){
            msg.setText(sMsg);
        }
        //如果自定义了按钮文本，则直接显示，没有则显示“取消”、“确定”
        if (!TextUtils.isEmpty(sNegative)){
            negative.setText(sNegative);
        }else {
            negative.setText("取消");
        }
        if (!TextUtils.isEmpty(sPositive)){
            positive.setText(sPositive);
        }else {
            positive.setText("确定");
        }
    }
    //初始化按钮监听器
    private void initEvent(){
        negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickButtonListener!=null){
                    onClickButtonListener.OnClickNegative();
                }
            }
        });
        positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickButtonListener.OnClickPositive();
            }
        });
    }
    public interface OnClickButtonListener{
        void OnClickNegative();         //实现“取消”点击事件的方法
        void OnClickPositive();         //实现“确定”点击事件的方法
    }
    //由于Dialog被创建后，要调用show（）方法才会回调onCreate()，所以重写show（）方法中调用refreshView（）初始化控件数据
    @Override
    public void show() {
        super.show();
        refreshView();
    }

    //设置点击事件的回调
    public OnClickButtonListener onClickButtonListener;
    public CustomDialog setOnClickButtonListener(OnClickButtonListener onClickButtonListener){
        this.onClickButtonListener=onClickButtonListener;
        return this;
    }

    public CustomDialog setTittle(String Tittle) {
        this.sTittle = Tittle;
        return this;
    }

    public CustomDialog setMsg(String Msg) {
        this.sMsg = Msg;
        return this;
    }

    public CustomDialog setNegative(String Negative) {
        this.sNegative = Negative;
        return this;
    }

    public CustomDialog setPositive(String Positive) {
        this.sPositive = Positive;
        return this;
    }

}
